/*
* Description: 电源管理
*/

#include "power_manager.h"
#include "common/session_utils.h"

using namespace COMMON;

namespace GUEST {
bool PowerManager::Init(std::shared_ptr<WINCONTROL::SendToHost> sendToHostSharedPtr)
{
    if (sendToHostSharedPtr == nullptr) {
        LOG_ERR("send to host shared ptr is null");
        return false;
    }
    sendToHost_ = sendToHostSharedPtr;
    SessionUtils::RegSessionChangeCallback(sessionName_,
        std::bind(&PowerManager::GetSessionState, &POWER_MANAGER, std::placeholders::_1));
    LOG_INFO("init power manager success");
    return true;
}

void PowerManager::GetSessionState(int32_t state)
{
    LOG_INFO("power manager get session state {}", state);
    if (WTS_SESSION_LOGON == state || WTS_SESSION_UNLOCK == state) {
        SendScreenOffMessageToHost(true);
        return;
    }
    if (WTS_SESSION_LOGOFF == state || WTS_SESSION_LOCK == state) {
        SendScreenOffMessageToHost(false);
    }
    return;
}

void PowerManager::Quit()
{
    SessionUtils::UnRegSessionChangeCallback(sessionName_);
}

void PowerManager::SendScreenOffMessageToHost(bool flag)
{
    if (sendToHost_ != nullptr) {
        sendToHost_->EnableOrDisableScreenLock(flag);
    }
}
} // namespace